package com.asa.common.net

import kotlinx.coroutines.coroutineScope
import java.lang.Exception

open class NetModel {

    suspend fun<T:Any> safeRequest(call:suspend (()->Result<T>)):Result<T>{
        return try {
            call.invoke()
        }catch (e:Exception){
            e.printStackTrace()
            Result.NetError(e)
        }
    }

    suspend fun<T:Any> executeResponse(response:BaseHttpResponse<T>):Result<T>{
        return coroutineScope {
            if (response.errorCode != 0){
                Result.ServiceError(response.errorCode,response.errorMsg)
            } else{
                if (response.data == null){
                    Result.SuccessNull(response.data)
                } else{
                    Result.Success(response.data!!)
                }
            }

        }
    }



}